<template>
  <div class="department-container">
    <el-tree :data="data" :props="defaultProps" :expand-on-click-node="false" default-expand-all>
      <template v-slot="{ data }">
        <el-row style="width: 100%">
          <el-col :span="20"><div>{{ data.name }}</div></el-col>
          <el-col :span="4"><div>
            <span class="re-name">{{ data.managerName }}</span>
            <el-dropdown :hide-on-click="false" @command="dropdownTmp($event, data.id)">
              <span class="el-dropdown-link">
                操作<i class="el-icon-arrow-down el-icon--right" />
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="add">增加子部门</el-dropdown-item>
                <el-dropdown-item command="edit">编辑</el-dropdown-item>
                <el-dropdown-item command="del">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div></el-col>
        </el-row>
      </template>
    </el-tree>

    <!-- 因为html规范的原因，标签不能有大写，只能有小写，
    所以一些组件，在js是大写，在html模板是 小写和破折号连接
     -->
    <AddEditDialog
      ref="dialogRef"
      :is-edit.sync="isEdit"
      :current-id="currentId"
      :show-dialog.sync="showDialog"
      :department-list="departmentList"
      @add-success="companyDepartmen"
    />
  </div>
</template>

<script>
import { companyDepartmentRequest, deleteDepartmentRequest } from '@/api/department'
import { transformListToTree } from '@/utils'
import AddEditDialog from './compoments/addEditDialog'
export default {
  name: 'DepartmentCom',
  components: {
    AddEditDialog
  },
  data() {
    return {
      data: [],
      departmentList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      currentId: '',
      isEdit: false

    }
  },
  created() {
    // 不要这created入口函数里面，写太多的逻辑代码，一般是放在methods里面去写函数
    // 然后再在created里面调用
    this.companyDepartmen()
  },
  methods: {
    async companyDepartmen() {
      const res = await companyDepartmentRequest()
      this.departmentList = res.data
      this.data = transformListToTree(res.data, 0)
    },
    dropdownTmp(type, id) {
      this.currentId = id
      if (type === 'add') {
        // 打开弹框， 但是变量又在弹窗里面，怎么办？
        this.showDialog = true
        this.isEdit = false
      } else if (type === 'del') {
        console.log(1)
        this.$confirm('是否确认删除', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await deleteDepartmentRequest(id)
          // 删除成功以后，要刷新部门列表（这个刷新就是调用接口，不是浏览器的刷新）
          this.companyDepartmen()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else {
        this.isEdit = true
        this.showDialog = true
      }
    },
    handleNodeClick(data) {
      console.log(data)
    }
  }
}
</script>

<style lang="scss">
.department-container{
  margin-top: 50px;
  margin-left: 150px;
  .el-dropdown-link {
    cursor: pointer;
  }
.el-icon-arrow-down {
    font-size: 12px;
  }
  .re-name{
    margin-right: 10px;
  }
}

</style>
